<?php include("includes/session.php"); ?>
<?php include("includes/connect_db.php"); ?>
<?php include("includes/header.php"); ?>
<?php include("notif.php"); ?>

<?php
	$id_kontak = $_GET['id'];

	if ($id_kontak == ""){
		$nama = "";
		$nomor = "";
		$email = "";
		$jenis_kelamin = "";
		$tanggal = "";
		$image = "";
		$alamat = "";
	} else{
		$sql_cari_kontak = "SELECT * FROM tb_kontak WHERE id_kontak='$id_kontak'";
		$query_cari_kontak = mysql_query($sql_cari_kontak);
		while($cari_kontak = mysql_fetch_array($query_cari_kontak)){
			$nama = $cari_kontak['nama_kontak'];
			$nomor = $cari_kontak['no_hp'];
			$email = $cari_kontak['email'];
			$jenis_kelamin = $cari_kontak['jenis_kelamin'];
			$tanggal = $cari_kontak['tanggal_lahir'];
			$image = $cari_kontak['image'];
			$alamat = $cari_kontak['alamat'];
			$id_kontak = $cari_kontak['id_kontak'];
		}
	}
?>

<style>
	div{
		font-family: 'SEGOE UI';
	}

	.frame{
		border: 1px solid #08c;
		width: 250px;
		height: 250px;
		margin-bottom: 5px;
	}

	#pilihGambar{
		width: 250px;
		text-align: center;
	}
</style>

<div class="span9">  
    <h3>SMS</h3>                      
    <ul class="breadcrumb well">
    	<li><a href="kontak.php">Contact</a> <span class="divider">/</span></li>
        <li class="active">Edit</li>                
    </ul>    
    <div class="alert alert-info">
		Edit Kontak
    </div>
    
	<form class="form-horizontal" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    	<div>
			<div class="span12">
				<div class="span4">
					<div class="frame">
						<img style="height: 250px;" src="<?php echo cekGambar($id_kontak, 1, 2); ?>">
					</div>
					<div>
						<button name="Gambar" id="pilihGambar" class="btn btn-info">Pilih Gambar</button>
					</div>
				</div>		

				<div class="span8">
					<div class="control-group">
						<div class="controls">
							<input placeholder="Masukkan Nama" type="hidden" name="id_kontak" value="<?php echo $id_kontak ?>">
						</div>
					</div>
					<div class="control-group">
						<label class="control-label">Nama</label>
						<div class="controls">
							<input placeholder="Masukkan Nama" type="text" name="nama" value="<?php echo $nama ?>">
							<i class="icon-ok-circle" style="opacity: 0.5"></i>
						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label">No. Telepon</label>
						<div class="controls">
							<input placeholder="Masukkan Nomor Telepon" type="text" name="nomor" value="<?php echo $nomor ?>">
							<i class="icon-ok-circle" style="opacity: 0.5"></i>
						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label">Email</label>
						<div class="controls">
							<input placeholder="Masukkan Email" type="email" name="email" value="<?php echo $email ?>">
						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label">Jenis Kelamin</label>
						<div class="controls">
							<input type="radio" value="l" name="jenis_kelamin"> Laki-Laki
					    	<input type="radio" value="p" name="jenis_kelamin"> Perempuan
						</div>
					</div>

					<div class="control-group">
						<label class="control-label">Alamat</label>
						<div class="controls">
							<textarea placeholder="Masukkan Alamat" name="alamat" style="height: 50px; overflow: hidden; word-wrap: break-word; resize: none;"><?php echo $alamat ?></textarea>
						</div>
					</div>
					
					<div class="control-group">
						<label class="control-label">Tanggal Lahir</label>
						<div class="controls">
							<input placeholder="Masukkan Tanggal Lahir" type="date" name="tanggal" value="<?php echo $tanggal ?>" class="datePicker">
						</div>
					</div>
				</div>
			</div>
			<div>
				<div class="span12">
					<ul class="breadcrumb well">
						<button type="submit" style="width: 80px;" class="btn btn-info" name="simpan">Simpan</button>
						<button type="submit" style="width: 80px;" class="btn btn-danger" name="batal">Batal</button>
					</ul>
				</div>
			</div>
		</div>
	</form>
</div>

<?php
	if(isset($_POST['simpan'])){
		$id_kontak = $_POST['id_kontak'];
		$nama = $_POST['nama'];
		$nomor = $_POST['nomor'];
		$email = $_POST['email'];
		$jenis_kelamin = $_POST['jenis_kelamin'];
		$tanggal = $_POST['tanggal'];
		$image = $_POST['image'];
		$alamat = $_POST['alamat'];


		//echo $nama."; ".$nomor."; ".$email."; ".$jenis_kelamin."; ".$tanggal."; ".$image."; ".$alamat;

		if (empty($nama) or empty($nomor)){
            echo "<SCRIPT> alert('Isi Data Terlebih Dahulu'); location.href='kontak-edit.php?id='+$id_kontak;</SCRIPT>";
        } elseif (!is_numeric($nomor)) {
        	echo "<SCRIPT> alert('Nomor Telepon Harus Angka'); location.href='kontak-edit.php?id='+$id_kontak;</SCRIPT>";
        } else{
        	if (empty($tanggal) and empty($jenis_kelamin)) {
        		$sql_input_kontak = "UPDATE tb_kontak
									SET nama_kontak = '$nama', no_hp = '$nomor', email = '$email', alamat = '$alamat'
									WHERE id_kontak = '$id_kontak'";	
        	} elseif (empty($tanggal)) {
        		$sql_input_kontak = "UPDATE tb_kontak
									SET nama_kontak = '$nama', no_hp = '$nomor', email = '$email', jenis_kelamin = '$jenis_kelamin', alamat = '$alamat'
									WHERE id_kontak = '$id_kontak'";
        	} elseif (empty($jenis_kelamin)) {
        		$sql_input_kontak = "UPDATE tb_kontak
									SET nama_kontak = '$nama', no_hp = '$nomor', email = '$email', tanggal_lahir = '$tanggal', alamat = '$alamat'
									WHERE id_kontak = '$id_kontak'";
        	} else{
		    	$sql_input_kontak = "UPDATE tb_kontak
									SET nama_kontak = '$nama', no_hp = '$nomor', email = '$email', jenis_kelamin = '$jenis_kelamin', tanggal_lahir = '$tanggal', alamat = '$alamat'
									WHERE id_kontak = '$id_kontak'";
			}

			$query_input_kontak = mysql_query($sql_input_kontak) or die(mysql_error());

			if ($query_input_kontak) {
				echo "<SCRIPT> alert('DATA DISIMPAN'); location.href='kontak.php';</SCRIPT>";
			} else {
				echo "<SCRIPT> alert('DATA GAGAL DISIMPAN');</SCRIPT>";
			}
	  	}
	} elseif (isset($_POST['batal'])) {
		echo "<SCRIPT> location.href='kontak.php';</SCRIPT>";
	}
?>

<?php include("includes/footer.php"); ?>

<script type="text/javascript">
	$(document).ready(function() {
		
        $('#pilihGambar').click(function(){
          var formProfil = {
          	tipe: 1,
            id: <?php echo $_GET["id"]; ?>
          };
          $.ajax({
            url: 'modal-profil.php',
            type: 'POST',
            data: formProfil,
            complete: function(xhr, textStatus) {
              //called when complete
            },
            success: function(data, textStatus, xhr) {
              $('#tampil-modal').html(data);
            },
            error: function(xhr, textStatus, errorThrown) {
              //called when there is an error
            }
          });
          return false;
        });
	});
</script>